const cloud = require('wx-server-sdk');
const md5=require('../mod/md5');

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
});

const db=cloud.database();

exports.main = async (event, context) => {
  const {username,password,name,idNumber,Class,grade,gender,college}=event.data;
  try{
    const repeat=await db.collection("student").where(
      {
        account:username
      }
    ).get();
    if(repeat.data.length==0){
      try{
        const str=md5.hexMD5(password);
        const up=await db.collection("student").add({
          data:{  
            name:name,
            gender:gender,
            college:college,
            Class:Class,
            idNumber:idNumber,
            account:account,
            password:str,
            remark:'',
            grade:grade,
            check_status:0,
            identity:0,
            family_background:'',
            political_thought:'',
            learning_condition:'',
            social_practice:'',
            innovation_ability:'',
            date:''
          }
        })
        // .then(res=>{
          return {success:true,message:"注册成功"}
        // }).catch(err=>{
        //   return {success:false,message:err}
        // })
          
      }catch(error){
          console.error(error);
          return{success:false,message:"注册失败"+error}
      }
    }
    else{
      return {success:false,message:"该用户已存在"}
    }
  }catch(error){
    return{success:false,message:"注册失败"}
  }
};


